var userName
var passWord
var rPassWord
var nickName
function Color(thiss, ze, ele) {
    if (ele === ``) {
        $(this).css({
            border: "1px solid pink"
        })
    } else if (ze.test(ele)) {
        $(thiss).css({
            border: "1px solid green"
        })
    } else if (!ze.test(ele)) {
        $(thiss).css({
            border: "1px solid red"
        })
    }
}
// 用户名
$('[name="a1"]').on("input", function () {
    userName = $('[name="a1"]')[0].value
    var a1 = /^[a-z0-9]\w{4,11}$/
    var thiss = this
    Color(thiss, a1, userName)

})
// 用户名失去焦点}
$('[name="a1"]').on("blur", function () {
    if (userName === `` || userName === undefined) {
        $(this).css({
            border: "1px solid red"
        })
    }
})
// 密码
$('[name="a2"]').on("input", function () {
    passWord = $('[name="a2"]')[0].value
    var a2 = /\w{6,12}/
    var thiss = this
    Color(thiss, a2, passWord)
    if (rPassWord !== passWord) {
        $('[name="a3"]').css({
            border: "1px solid red"
        })
    }
})
$('[name="a2"]').on("blur", function () {
    if (passWord === `` || passWord === undefined) {
        $(this).css({
            border: "1px solid red"
        })
    }
})
// 确认密码
$('[name="a3"]').on("input", function () {
    rPassWord = $('[name="a3"]')[0].value
    var thiss = this
    if (rPassWord === ``) {
        $(this).css({
            border: "1px solid pink"
        })
    } else if (rPassWord === passWord) {
        $(this).css({
            border: "1px solid green"
        })
    } else if (rPassWord !== passWord) {
        $(this).css({
            border: "1px solid red"
        })
    }
})
$('[name="a3"]').on("blur", function () {
    if (rPassWord === `` || rPassWord === undefined) {
        $(this).css({
            border: "1px solid red"
        })
    }
})
// 昵称
$('[name="a4"]').on("input", function () {
    nickName = $('[name="a4"]')[0].value

    if (nickName === ``) {
        $(this).css({
            border: "1px solid pink"
        })
    } else {
        $(this).css({
            border: "1px solid green"
        })
    }
})
$('[name="a4"]').on("blur", function () {
    if (nickName === `` || nickName === undefined) {
        $(this).css({
            border: "1px solid red"
        })
    }
})
// 表单提交
$("form").on("submit", function () {
    if ($('[name="a1"]').css("border-color") === "rgb(0, 128, 0)" && $('[name="a2"]').css("border-color") === "rgb(0, 128, 0)"
        && $('[name="a3"]').css("border-color") === "rgb(0, 128, 0)" && $('[name="a4"]').css("border-color") === "rgb(0, 128, 0)") {
        console.log(`满足提交条件` + `用户名` + userName, `密码` + passWord, `确认密码` + rPassWord, `昵称` + nickName);
        promiseAjax({
            url: "http://localhost:8888/users/register",
            method: "post",
            data: {
                username: userName,
                password: passWord,
                rpassword: rPassWord,
                nickname: nickName,
            },
            headers: {
                "content-type": "application/x-www-form-urlencoded"
            }
        }).then(function (res) {
            console.log(res);
            if (res.code === 1) {
                alert(res.message)
                location.href = "login.html"
            }else{
                alert(res.message)
            }
        })
    } else {
        console.log("不满足提交条件");
        alert("请检查注册内容")
    }
    return false
})
$(".login").on("click",function(){
    location.href="login.html"
})
$(".login").hover(function(){
    $(this).css({
        color:'#e31436',
    })
},function(){
    $(this).css({
        color:" #666",
    })
})
